const { Application } = require('ee-core');
const { app, BrowserWindow, Menu, MenuItem, ipcMain, globalShortcut } = require('electron')

class Index extends Application {

  constructor() {
    super();
    // this === eeApp;
  }

  /**
   * core app have been loaded
   */
  async ready () {
    // do some things
  }

  /**
   * electron app ready
   */
  async electronAppReady () {
    // do some things
  }

  /**
   * main window have been loaded
   */
  async windowReady () {
    // do some things
    // 延迟加载，无白屏
    const winOpt = this.config.windowsOption;
    if (winOpt.show == false) {
      const win = this.electron.mainWindow;
      win.once('ready-to-show', () => {
        win.show();
      })
    }
    //
    let openorclosekey = 'H'
    const menu = new Menu()
    menu.append(new MenuItem({
      label: 'Electron',
    }))
    Menu.setApplicationMenu(menu)
    //
    // 监听来自页面的快捷键触发消息
    ipcMain.on('shortcut-triggered', (event, key) => {
      // 在主进程中处理传递的快捷键键值
      const win = this.electron.mainWindow;
      win.isVisible() ? win.hide() : win.show()
      openorclosekey = key
      // console.log(`快捷键触发：${key}`,openorclosekey);
      globalShortcut.register('Control+' + openorclosekey, () => {
        console.log('Electron loves global shortcuts!')
        const win = this.electron.mainWindow;
        win.isVisible() ? win.hide() : win.show()
      })
    });
    


  }

  /**
   * before app close
   */  
  async beforeClose () {
    // do some things

  }
}

Index.toString = () => '[class Index]';
module.exports = Index;